////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   N/A
// AUTHOR           :   Victor Zheng
// DATE CREATE      :   2006/06/28
// DESCRIPTION      :   a Print Form Utility File
//                  :
//                  :
//                  :
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2006/06/28   Victor Zheng    1.0                 source is copied from ANCO.
// 2006/07/04   Jerry Chen      1.0                 Code enhancemnts: modify setPdfDocPassword function for
//														 module VENDORCONTRACT_GARMENT
// 2006/07/13   Jerry Chen      1.0                 Code enhancemnts: modify setPdfDocPassword function for
//														 module VENDORCONTRACT_FOOTWEAR
// 2006/07/31   Jerry Chen      1.0                 BugZilla bug6493: add method for data format
//                                                        and remove the useless package.
// 2006/08/15   Jerry Chen      1.0                 BugZilla bug6193: add Encryption for the pdf document
////////////////////////////////////////////////////////////////////////////////

package com.erp.app.util.web;

import java.math.BigDecimal;
import java.math.BigInteger;

import javax.servlet.http.HttpSession;

import com.erp.app.admin.companySetup.vo.CompanyVo;
import com.erp.app.common.ProjectConstants;
import com.erp.app.common.select.beans.SelectDelegate;
import com.erp.framework.SimpleConstants;
import com.erp.framework.SimpleConstants.DateFormats;
import com.erp.framework.types.CustomSqlDate;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;
import com.lowagie.text.pdf.PdfWriter;

public class PrintFormUtil {

//   public static String convertToPdfDateFormat(String inputDate) {
//        if (CommonUtil.isEmpty(inputDate) || !CommonUtil.isValidDate(inputDate, DateFormats.INPUT_DATE_FORMAT)) {
//            return "";
//        }
//        Date date = CommonUtil.stringToDate(inputDate, DateFormats.INPUT_DATE_FORMAT);
//        return CommonUtil.dateToString(date, ProjectConstants.PRINT_FORM_DATE_FORMAT);
//    }
	public static String getCompanyDetailAddress(CompanyVo vo,SelectDelegate sdg) {//Avoid to call DB so many times
        String companyAddress = "";
        if (!CommonUtil.isEmpty(vo.getAddr1())) {
            companyAddress = vo.getAddr1();
        }
        if (!CommonUtil.isEmpty(vo.getAddr2())) {
            companyAddress += CommonUtil.isEmpty(companyAddress) ? "" : ", ";
            companyAddress += vo.getAddr2();
        }
        if (!CommonUtil.isEmpty(vo.getAddr3())) {
            companyAddress += CommonUtil.isEmpty(companyAddress) ? "" : ", ";
            companyAddress += vo.getAddr3();
        }
        if (!CommonUtil.isEmpty(vo.getAddr4())) {
            companyAddress += CommonUtil.isEmpty(companyAddress) ? "" : ", ";
            companyAddress += vo.getAddr4();
        }
        if(sdg!=null){
            if (!CommonUtil.isEmpty(vo.getCountry())) {
                companyAddress += CommonUtil.isEmpty(companyAddress) ? "" : ", ";
                companyAddress += CommonUtil.nullToString(getCodeDesc(sdg,vo.getCompanyCode(),ProjectConstants.CD_COUNTRY,vo.getCountry()));
            }
      }

        return companyAddress;
    }

    public static String getAddress(String address1, String address2, String address3, String address4,  String countryName){
        String reAddress = "";
        if (!CommonUtil.isEmpty(address1)) {
            reAddress = address1;
        }
        if (!CommonUtil.isEmpty(address2)) {
            reAddress += CommonUtil.isEmpty(reAddress) ? "" : ", ";
            reAddress += address2;
        }
        if (!CommonUtil.isEmpty(address3)) {
            reAddress += CommonUtil.isEmpty(reAddress) ? "" : ", ";
            reAddress += address3;
        }
        if (!CommonUtil.isEmpty(address4)) {
            reAddress += CommonUtil.isEmpty(reAddress) ? "" : ", ";
            reAddress += address4;
        }
        if (!CommonUtil.isEmpty(countryName)) {
            reAddress += CommonUtil.isEmpty(reAddress) ? "" : ", ";
            reAddress += CommonUtil.isEmpty(countryName);
        }
        return reAddress;
    }

    public static String formatCurrentPDFDate(CustomSqlDate date) {
        return date != null ? CommonUtil.dateToString(date, SimpleConstants.PDF_CURRENT_DATE_FORMAT) : "";
    }
    public static String formatCurrentPDFDatetime(CustomSqlDate date) {
        return date != null ? CommonUtil.dateToString(date, SimpleConstants.PDF_CURRENT_DATETIME_FORMAT) : "";
    }
    public static String formatDefaultPDFDate(CustomSqlDate date) {
        return date != null ? CommonUtil.dateToString(date, DateFormats.OUTPUT_DATE_LONG_FORMAT) : "";
    }
    public static String formatPDFDateWithYMDBIAS(CustomSqlDate date) {
        return date != null ? CommonUtil.dateToString(date, DateFormats.OUTPUT_DATE_YMD_WITH_BIAS_FORMAT) : "";
    }

    public static String formatPDFDateWithMDYBIAS(CustomSqlDate date) {
        return date != null ? CommonUtil.dateToString(date, DateFormats.OUTPUT_DATE_MDY_WITH_BIAS_FOMAT) : "";
    }

    public static BigDecimal roundAmount(BigDecimal data) {
        return (data != null) ? data.setScale(ProjectConstants.FORMAT_AMOUNT_DEFAULT_SCALE, BigDecimal.ROUND_HALF_UP)
                : new BigDecimal(0);
    }

    public static String formatDefaultPDFInteger(BigDecimal data){
        return CommonUtil.formatDefaultInteger(data);
    }

    public static String formatDefaultPDFDecimal(BigDecimal data){
        return CommonUtil.formatDefaultDecimal(data);
    }

    public static String formatDefaultPDFAmount(BigDecimal data){
        return CommonUtil.formatDefaultAmount(data);
    }

    public static String formatDefaultPDFMeasurement(BigDecimal inNum) {
        return CommonUtil.formatDefaultMeasurement(inNum);
    }

    public static String formatDefaultPDFPrice(BigDecimal inNum) {
        return CommonUtil.formatDefaultPrice(inNum);
    }

    public static String formatDefaultPDFPercent(BigDecimal inNum) {
        return CommonUtil.formatDefaultPercent(inNum);
    }

    public static String formatDefaultPDFInteger(String inNum) {
        return CommonUtil.formatDefaultInteger(inNum);
    }

    public static String formatDefaultPDFDecimal(String inNum) {
        return CommonUtil.formatDefaultDecimal(inNum);
    }

    public static String formatDefaultPDFAmount(String inNum) {
        return CommonUtil.formatDefaultAmount(inNum);
    }

    public static String formatDefaultPDFMeasurement(String inNum) {
        return CommonUtil.formatDefaultMeasurement(inNum);
    }

    public static String formatDefaultPDFPrice(String inNum) {
        return CommonUtil.formatDefaultPrice(inNum);
    }

    public static String formatDefaultPDFPercent(String inNum) {
        return CommonUtil.formatDefaultPercent(inNum);
    }

    public static void setPdfDocPassword(PdfWriter writer, HttpSession session, String docType){
        try{
            String pdfUserPassword = "";

            //PDF Owner Password, not much useful, just for the sake of the parameter in API call
            String pdfOwnerPassword = "";

            //permissions can be: PdfWriter.AllowPrinting, PdfWriter.AllowModifyContents, PdfWriter.AllowCopy, PdfWriter.AllowModifyAnnotations
            //, PdfWriter.AllowFillIn, PdfWriter.AllowScreenReaders, PdfWriter.AllowAssembly and PdfWriter.AllowDegradedPrinting
            int permissions = PdfWriter.AllowCopy | PdfWriter.AllowPrinting | PdfWriter.AllowModifyContents;

            //if pdfUserPassword <> 'NO', then set the user password for the PDF document
            if(!CommonUtil.nullToString(pdfUserPassword).toUpperCase().trim().equals(ProjectConstants.GENERIC_NO)){
                writer.setEncryption(PdfWriter.STRENGTH128BITS, pdfUserPassword, pdfOwnerPassword, permissions);
            }
        }catch(Exception e){
            Logger.logExceptions(e);
        }
    }

    //////////////////////////////////////////////////////////////////////////
    ////The following function is to get the description from other table/////
    //////////////////////////////////////////////////////////////////////////
    public static String getUserName(SelectDelegate sdg, String companyCode, String userCode) {
        if(!CommonUtil.isEmpty(userCode) && !CommonUtil.isEmpty(companyCode)){
            String query =" SELECT USER_NAME FROM T_USER WHERE COMPANY_CODE='" + companyCode
            + "' AND USER_CODE='" + userCode + "' AND ACTIVATION = '"+ProjectConstants.GENERIC_ACTIVE+"' ";
            String rslt = String.valueOf(sdg.selectOneValue(query));
            return rslt.equals("null")? "":rslt;
        }
        return "";
    }

    public static String getCodeDesc(SelectDelegate sdg, String companyCode, String refMasterCode, String refCode) {
        if(!CommonUtil.isEmpty(companyCode) && !CommonUtil.isEmpty(refMasterCode) && !CommonUtil.isEmpty(refCode)){
            String query = "select decode(nvl(hdr.SHOW_DESC, '"+ProjectConstants.GENERIC_YES+"'), '"+ProjectConstants.GENERIC_YES+"', dtl.REF_DESC, dtl.REF_CODE) "
	            + " from T_CODE_HDR hdr, T_CODE_DTL dtl "
	            + " where hdr.REF_MASTER_CODE='" + refMasterCode+ "' "
	            + " AND hdr.COMPANY_CODE = '"+companyCode +"' "
	            + " AND hdr.COMPANY_CODE = dtl.COMPANY_CODE "
	            + " AND hdr.REF_MASTER_CODE = dtl.REF_MASTER_CODE "
	            + " AND dtl.REF_CODE = '"+refCode+"'";
		        String rslt = String.valueOf(sdg.selectOneValue(query));
	        return rslt.equals("null")? "":rslt;
      }
      return "";
    }

    public static String getCodeDescNoDepend(SelectDelegate sdg, String companyCode, String refMasterCode, String refCode) {
        if(!CommonUtil.isEmpty(companyCode) && !CommonUtil.isEmpty(refMasterCode) && !CommonUtil.isEmpty(refCode)){
            String query = "select  dtl.REF_DESC "
            + " from T_CODE_HDR hdr, T_CODE_DTL dtl "
            + " where hdr.REF_MASTER_CODE='" + refMasterCode+ "' "
            + " AND hdr.COMPANY_CODE = '"+companyCode +"' "
            + " AND hdr.COMPANY_CODE = dtl.COMPANY_CODE "
            + " AND hdr.REF_MASTER_CODE = dtl.REF_MASTER_CODE "
            + " AND dtl.REF_CODE = '"+refCode+"'";
        String rslt = String.valueOf(sdg.selectOneValue(query));
        return rslt.equals("null")? "":rslt;
        }
      return "";
    }

    public static byte[] getCompanyLogo(String companyCode){
        String query = "select company_logo from t_company where company_code = '" + companyCode + "' ";
        SelectDelegate selectDelegate = new SelectDelegate();
        byte[] byteImage = selectDelegate.selectOneImage(query);
        return byteImage;
    }

    ///The following funtion will be delete in a short time
    public static String getCompanyDetailAddress(SelectDelegate sdg, String companyCode) {//This function will be removed in a short time
        String companyAddress = null;
        if (!getCompanyContact(sdg, companyCode, "ADDR1").equals("")) {
            companyAddress = getCompanyContact(sdg, companyCode, "ADDR1");
        }
        if (!getCompanyContact(sdg, companyCode, "ADDR2").equals("")) {
            if (companyAddress != null) {
                companyAddress = companyAddress + ", " + getCompanyContact(sdg, companyCode, "ADDR2");
            }
            else companyAddress = getCompanyContact(sdg, companyCode, "ADDR2");
        }
        if (!getCompanyContact(sdg, companyCode, "ADDR3").equals("")) {
            if (companyAddress != null) {
                companyAddress = companyAddress + ", " + getCompanyContact(sdg, companyCode, "ADDR3");
            }
            else companyAddress = getCompanyContact(sdg, companyCode, "ADDR3");
        }
        if (!getCompanyContact(sdg, companyCode, "ADDR4").equals("")) {
            if (companyAddress != null) {
                companyAddress = companyAddress + ", " + getCompanyContact(sdg, companyCode, "ADDR4");
            }
            else companyAddress = getCompanyContact(sdg, companyCode, "ADDR4");
        }
        if (!getCompanyContact(sdg, companyCode, "COUNTRY").equals("")) {
            if (companyAddress != null) {
                companyAddress = companyAddress + ", " +getCodeDesc(sdg,companyCode,ProjectConstants.CD_COUNTRY,getCompanyContact(sdg, companyCode, "COUNTRY"));
            }
            else companyAddress = getCodeDesc(sdg,companyCode,ProjectConstants.CD_COUNTRY,getCompanyContact(sdg, companyCode, "COUNTRY"));
        }

        return companyAddress.equals("null")? "":companyAddress;
    }

    public static String getCompanyContact(SelectDelegate sdg, String companyCode, String colName) {
        if(!CommonUtil.isEmpty(companyCode) && !CommonUtil.isEmpty(colName)){
             String query = " SELECT " + colName + " FROM T_COMPANY"
             + " WHERE COMPANY_CODE = '" + companyCode + "' ";
             String rslt = String.valueOf(sdg.selectOneValue(query));
             return rslt.equals("null")? "":rslt;
        }
        return "";
    }

    public static String getCompanyLongName(SelectDelegate sdg, String companyCode) {
        if(!CommonUtil.isEmpty(companyCode)){
             String query = "select LONG_NAME from t_company "
                    + " where company_code = '"+companyCode+"' ";
                String rslt = String.valueOf(sdg.selectOneValue(query));
                return rslt.equals("null")? "":rslt;
        }
        return "";
    }
    
    public static String formatFooterPageNo(int pageNo){
        return ProjectConstants.PDF_GENERIC_LABEL_PAGE + " " + pageNo + " " + ProjectConstants.PDF_GENERIC_LABEL_OF + " ";
    }

}